<!--
 * FCKeditor - The text editor for internet
 * Copyright (C) 2003-2005 Frederico Caldeira Knabben
 * 
 * Licensed under the terms of the GNU Lesser General Public License:
 * 		http://www.opensource.org/licenses/lgpl-license.php
 * 
 * For further information visit:
 * 		http://www.fckeditor.net/
 * 
 * File Name: frmresourceslist.html
 * 	This page shows all resources available in a folder in the File Browser.
 * 
 * File Authors:
 * 		Frederico Caldeira Knabben (fredck@fckeditor.net)
 * 		Grant French (grant@mcpuk.net)
-->
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">
<html>
	<head>
		<link href="browser.css" type="text/css" rel="stylesheet">
		<script type="text/javascript" src="js/fckxml.js"></script>
		<script type="text/javascript" src="js/common.js"></script>
		<script language="javascript">

var oListManager = new Object() ;

oListManager.Init = function()
{
	this.Table = document.getElementById('tableFiles') ;
}

oListManager.Clear = function()
{
	// Remove all other rows available.
	while ( this.Table.rows.length > 0 )
		this.Table.deleteRow(0) ;
}

oListManager.AddFolder = function( folderName, folderPath )
{
	var docWidth=document.body.clientWidth;
	if (docWidth==null) docWidth=document.body.scrollWidth;
	var maxCols=docWidth/120;
	maxCols--;
	
	var rows=this.Table.getElementsByTagName('tr');
	
	
	if (rows.length!=0) {
		var lastRow = rows[(rows.length-1)];
		var cells = lastRow.getElementsByTagName('td');
		var lastRow_Cols = cells.length;
		
		if (lastRow_Cols>=maxCols) {
			lastRow = this.Table.insertRow(-1);
			lastRow.className="Frame_tb";
		}
	} else {
		var lastRow = this.Table.insertRow(-1);
		lastRow.className="Frame_tb";
	}
	
	// Build the link to view the folder.
	var sLink = '<a href="#" onclick="OpenFolder(\'' + escape(folderPath) + '\');return false;">' ;
	var dLink = '<a href="#" onclick="DeleteFolder(\'' + escape(folderName) + '\');return false;">' ;
	var rLink = '<a href="#" onclick="RenameFolder(\'' + escape(folderName) + '\');return false;">' ;
	
	// Add the file icon cell.
	var oCell = lastRow.insertCell(-1) ;
	oCell.vAlign="top";
	oCell.width = 96 ;
	oCell.innerHTML = sLink + '<img alt="' + folderName + '" src="images/Folder96.jpg" width="96" height="96" border="0"><br />' + folderName + '</a><br />';
	oCell.innerHTML += dLink + '<img border="0" src="images/delete.gif" width="16" height="16" alt="Delete Folder" title="Delete Folder"></a>&nbsp;';
	oCell.innerHTML += rLink + '<img border="0" src="images/edit.gif" width="16" height="16" alt="Rename Folder" title="Rename Folder"></a>' ;
	oCell.noWrap = true ;
	oCell.align = 'center' ;
	
}

oListManager.AddFile = function ( fileName, fileUrl, fileSize, imageEditable )
{
	var docWidth=document.body.clientWidth;
	if (docWidth==null) docWidth=document.body.scrollWidth;
	var maxCols=docWidth/120;
	maxCols--;
	
	var rows=this.Table.getElementsByTagName('tr');
	
	
	if (rows.length!=0) {
		var lastRow = rows[(rows.length-1)];
		var cells = lastRow.getElementsByTagName('td');
		var lastRow_Cols = cells.length;
		
		if (lastRow_Cols>=maxCols) {
			var lastRow = this.Table.insertRow(-1);
			lastRow.className="Frame_tb";
		}
	} else {
		var lastRow = this.Table.insertRow(-1);
		lastRow.className="Frame_tb";
	}
	
	// Build the link to view the file.
	var sLink = '<a href="#" title="Size: ' + fileSize + 'kb" onclick="OpenFile(\'' + escape(fileUrl) + '\');return false;">' ;
	var dLink = '<a href="#" title="Delete file" onclick="DeleteFile(\'' + escape(fileName) + '\');return false;">' ;
	var eLink = '<a href="#" title="Rename file" onclick="RenameFile(\'' + escape(fileName) + '\');return false;">' ;
	var eImageLink = '<a href="#" title="Edit Image" onclick="EditImage(\'' + escape(fileName) + '\',\'' + escape(fileUrl) + '\');return false;">' ;
	
	// Add the file icon cell.
	var oCell = lastRow.insertCell(-1) ;
	oCell.vAlign="top";
	oCell.width = 96 ;
	
	oCell.innerHTML = sLink + 
		'<img alt="' + fileName + '" src="' + 
					oConnector.ConnectorUrl + 
					'?Command=Thumbnail&Type=' + 
					oConnector.ResourceType + 
					'&CurrentFolder=' + 
					oConnector.CurrentFolder + 
					'&FileName=' + escape(fileName) + 
					'&ExtraParams=' + oConnector.ExtraParams +
					'" width="96" height="96" border="0"><br />' + 
		fileName + '</a><br />';
	oCell.innerHTML += dLink + '<img border="0" src="images/delete.gif" width="16" height="16" alt="Delete File" title="Delete File"></a>&nbsp;';
	oCell.innerHTML += eLink + '<img border="0" src="images/edit.gif" width="16" height="16" alt="Edit/Rename File" title="Rename File"></a>' ;
	if (imageEditable==1) { 
		oCell.innerHTML += eImageLink + '<img border="0" src="images/editImage.gif" width="16" height="16" alt="Edit Image" title="Edit Image"></a>' ;
	}
		
	oCell.noWrap = true ;
	oCell.align = 'center' ;
	
}

oListManager.PackRow = function ()
{
	var docWidth=document.body.clientWidth;
	if (docWidth==null) docWidth=document.body.scrollWidth;
	var maxCols=docWidth/120;
	maxCols--;
	
	var rows=this.Table.getElementsByTagName('tr');
	
	
	if (rows.length!=0) {
		var lastRow = rows[(rows.length-1)];
		var cells = lastRow.getElementsByTagName('td');
		var lastRow_Cols = cells.length;
		
		if (lastRow_Cols<maxCols) {
			for (i=lastRow_Cols;i<maxCols;i++) {
				var oCell = lastRow.insertCell(-1);
				oCell.width=96;
				oCell.innerHTML = '&nbsp;';
				oCell.noWrap=true;
				oCell.align='center';
			}
		}
	}
	
}

function OpenFolder( folderPath )
{
	// Load the resources list for this folder.
	window.parent.frames['frmFolders'].LoadFolders( folderPath ) ;
}

function OpenFile( fileUrl )
{
	fileUrl=unescape(fileUrl);
	window.top.opener.SetUrl( fileUrl ) ;
	window.top.close() ;
	window.top.opener.focus() ;
}

function DeleteFile( fileName, fileUrl )
{
	if (confirm('Are you sure you wish to delete ' + unescape(fileName) + '?')) {
		oConnector.SendCommand( 'DeleteFile', "FileName=" + fileName, DeleteFileCallBack ) ;
	}
}

function DeleteFolder( folderName )
{
	if (confirm('Are you sure you wish to delete ' + unescape(folderName) + ' and all files in it?')) {
		oConnector.SendCommand( 'DeleteFolder', "FolderName=" + folderName, DeleteFolderCallBack ) ;
	}
}

function RenameFile( fileName, fileUrl )
{
	var promptMsg='Please enter the new name for ' + unescape(fileName) + ':';
	var newName=prompt(promptMsg,unescape(fileName));
	
	if ((newName!=null)&&(newName!='')&&(newName!=fileName)) {
		oConnector.SendCommand( 'RenameFile', "FileName=" + fileName + "&NewName=" + escape( newName ), RenameFileCallBack ) ;
	}
}

function EditImage( fileName, fileUrl )
{

	window.location.href="frmimageeditor.html?Connector=" + oConnector.ConnectorUrl + 
				"&Type=" + oConnector.ResourceType + 
				"&CurrentFolder=" + oConnector.CurrentFolder + 
				"&FileName=" + fileName + 
				"&FileURL=" + fileUrl;
				
}

function RenameFolder( folderName )
{
	var promptMsg='Please enter the new name for ' + unescape(folderName) + ':';
	var newName=prompt(promptMsg,unescape(folderName));
	
	if ((newName!=null)&&(newName!='')&&(newName!=unescape(folderName))) {
		oConnector.SendCommand( 'RenameFolder', "FolderName=" + folderName + "&NewName=" + escape ( newName ), RenameFolderCallBack ) ;
	}
}

function LoadResources( resourceType, folderPath )
{
	oListManager.Clear() ;
	oConnector.ResourceType = resourceType ;
	oConnector.CurrentFolder = folderPath
	oConnector.SendCommand( 'GetFoldersAndFiles', null, GetFoldersAndFilesCallBack ) ;
}

function Refresh()
{
	LoadResources( oConnector.ResourceType, oConnector.CurrentFolder ) ;
}

function GetFoldersAndFilesCallBack( fckXml )
{
	// Get the current folder path.
	var oNode = fckXml.SelectSingleNode( 'Connector/CurrentFolder' ) ;
	var sCurrentFolderPath	= oNode.attributes.getNamedItem('path').value ;
	var sCurrentFolderUrl	= oNode.attributes.getNamedItem('url').value ;

	// Add the Folders.	
	var oNodes = fckXml.SelectNodes( 'Connector/Folders/Folder' ) ;
	
	if (oNodes!=null) {
		for ( var i = 0 ; i < oNodes.length ; i++ )
		{
			var sFolderName = oNodes[i].attributes.getNamedItem('name').value ;
			oListManager.AddFolder( sFolderName, sCurrentFolderPath + sFolderName + "/" ) ;
		}
	}
	
	// Add the Files.	
	var oNodes = fckXml.SelectNodes( 'Connector/Files/File' ) ;
	if (oNodes!=null) {
		for ( var i = 0 ; i < oNodes.length ; i++ )
		{
			var sFileName = oNodes[i].attributes.getNamedItem('name').value ;
			var sFileSize = oNodes[i].attributes.getNamedItem('size').value ;
			var iEditable = oNodes[i].attributes.getNamedItem('editable') ;
			if (iEditable!=null) {
				iEditable = iEditable.value;	
			} else {
				iEditable = 0;
			}
			
			oListManager.AddFile( sFileName, sCurrentFolderUrl + sFileName, sFileSize, iEditable ) ;
		}
	}
	
	oListManager.PackRow();
}

function DeleteFileCallBack ( fckXml )
{
	var oNodes = fckXml.SelectNodes( 'Connector/Error' );
	if (oNodes!=null && oNodes.length>0) {
		var errNo = parseInt(oNodes[0].attributes.getNamedItem('number').value) ;
		
		switch (errNo) {
			case 0 :
				break;	
			
			case 302 :
				alert('Failed to delete file.');
				break;
			
			default:
				alert('DFi: Invalid XML response from connector..');
		}
	} else {
		alert('DFi: Invalid XML response from connector.');
	}
	Refresh();
}


function DeleteFolderCallBack ( fckXml )
{
	var oNodes = fckXml.SelectNodes( 'Connector/Error' );
	if (oNodes!=null && oNodes.length>0) {
		var errNo = parseInt(oNodes[0].attributes.getNamedItem('number').value) ;
		
		switch (errNo) {
			case 0 :
				break;	
			
			case 402 :
				alert('Failed to delete folder.');
				break;
			
			default:
				alert('DFo: Invalid XML response from connector..');
		}
	} else {
		alert('DFo: Invalid XML response from connector.');
	}
	Refresh();
}

function RenameFileCallBack ( fckXml )
{
	var oNodes = fckXml.SelectNodes( 'Connector/Error' );
	if (oNodes!=null && oNodes.length>0) {
		var errNo = parseInt(oNodes[0].attributes.getNamedItem('number').value) ;
		
		switch (errNo) {
			case 0 :
				break;	
			
			case 502 :
				alert('Failed to rename file.');
				break;
			
			default:
				alert('RFi: Invalid XML response from connector..');
		}
	} else {
		alert('RFi: Invalid XML response from connector.');
	}
	Refresh();
}


function RenameFolderCallBack ( fckXml )
{
	var oNodes = fckXml.SelectNodes( 'Connector/Error' );
	if (oNodes!=null && oNodes.length>0) {
		var errNo = parseInt(oNodes[0].attributes.getNamedItem('number').value) ;
		
		switch (errNo) {
			case 0 :
				break;	
			
			case 602 :
				alert('Failed to rename folder.');
				break;
			
			default:
				alert('RFo: Invalid XML response from connector..');
		}
	} else {
		alert('RFo: Invalid XML response from connector.');
	}
	Refresh();
}

window.onload = function()
{
	oListManager.Init() ;
	window.top.IsLoadedResourcesList = true ;
}
		</script>
	</head>
	<body class="FileArea" bottomMargin="10" leftMargin="10" topMargin="10" rightMargin="10">
		<table id="tableFiles" cellSpacing="4" cellPadding="5" width="100%" border="0">
		</table>
	</body>
</html>
